/* eslint no-unused-vars:0 */
import React from 'react';

import PubSub from 'pubsub-js';
import './../index.less';

import { Link } from 'react-router-dom';
import { Row, Col, Icon, Upload, message } from 'antd';
import { Table, Button, Popconfirm } from 'antd';

const url = 'reportFile';
export default class Report extends React.Component {
    constructor(props) {
        super(props);
    }

    state = {
        data: [],
        columns: [
            {
                title: '报告文档名称',
                key: 'originalName',
                inputType: 'input',
                dataIndex: 'originalName',
                editable: true
            },
            {
                title: '操作',
                key: 'reportFile',
                render: (text, record) => {
                    return (
                        <div>
                            <a onClick={this.readPdf.bind(text, record)}>
                                看文档内容
                            </a>
                        </div>
                    );
                }
            },
            {
                title: '操作',
                key: 'action',
                render: (text, record) => {
                    return (
                        <div>
                            <Popconfirm
                                title={'确定要删除这条数据?'}
                                onConfirm={() =>
                                    this.handleDelete(text, record)
                                }>
                                <a>删除</a>
                            </Popconfirm>
                        </div>
                    );
                }
            }
        ],
        visible: false,
        report: ''
    };

    componentDidMount() {
        let temp = sessionStorage.getItem('showReportFile');
        this.setState({
            report: temp
        });
        this.getData(temp);
    }

    getData = (message) => {
        this.$post(url + '/getByReport', {
            report: message,
            page: 1,
            size: 10
        })
            .then((res) => {
                if (res) {
                    var data = res.result.content;
                    for (var i = 0; i < data.length; i++) {
                        data[i]['key'] = data[i].id;
                    }
                    this.setState({ data: data });
                } else this.$showNotification('error', '获取数据失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    readPdf = (text, record) => {
        PubSub.publish('readPdf', text.saveName);
        sessionStorage.setItem('readPdf', text.saveName);
    };

    handleDelete = (data, record) => {
        this.$post(url + '/delete', data.id)
            .then((res) => {
                if (res) {
                    this.$showNotification('success', '删除成功');
                    this.getData(this.state.report);
                } else this.$showNotification('error', '删除失败');
            })
            .catch((e) => {
                console.log(e);
            });
    };

    handleCancel = () => {
        this.setState({
            visible: false
        });
    };

    handleChange = (info) => {
        let fileList = info.fileList.slice();
        let file = fileList[fileList.length - 1];

        console.log(file);
        console.log(fileList);

        if (file.status !== 'uploading') {
            console.log(file, fileList);
        }
        if (file.status === 'done') {
            if (file.response) {
                var response = file.response;
                if (response.code === 0) {
                    file.status = 'success';
                    message.success(`${file.name} 文件上传成功`);
                } else {
                    file.status = 'error';
                    message.success(`${file.name} 文件上传失败`);
                }
            }
        }

        this.setState({
            fileList: fileList
        });

        this.getData(this.state.report);
    };

    render() {
        const props = {
            name: 'file',
            action: 'http://192.168.99.125:8848/reportFile/upload/',
            data: {
                report: this.state.report
            },
            headers: {},
            onChange: this.handleChange
        };

        return (
            <div className="body">
                <span className="opinion-body">舆情报告管理</span>
                <Row />
                <Row className="overview-row2">
                    <Upload {...props} fileList={this.state.fileList}>
                        <Button>
                            <Icon type="upload" /> 上传报告
                        </Button>
                    </Upload>
                    <Table
                        columns={this.state.columns}
                        dataSource={this.state.data}
                    />
                </Row>
            </div>
        );
    }
}
